package cn.spike.rockcc.mq;

import cn.spike.rockcc.domain.MessageStation;
import cn.spike.rockcc.dto.StationDto;
import cn.spike.rockcc.service.IMessageSmsService;
import cn.spike.rockcc.service.IMessageStationService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author:spike
 * @create:2023-03-17 18:07
 */
@RocketMQMessageListener(consumerGroup = "service-consumer-station",
                        topic = "course-topic",
                        selectorExpression = "course-publish")
@Component
public class StationConsumer implements RocketMQListener<String> {
    @Autowired
    private IMessageStationService messageStationService;

    @Override
    public void onMessage(String s) {
        StationDto stationDto = JSON.parseObject(s, StationDto.class);
        MessageStation station = new MessageStation();
        BeanUtils.copyProperties(stationDto,station);
        station.setIsread(0);
        station.setSendTime(new Date());
        stationDto.getUserIds().forEach(userId->{
            station.setUserId(userId);
            messageStationService.insert(station);
            station.setId(null);
        });
    }
}
